package pe.gob.osce.sirenas.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import pe.gob.osce.sirenas.dao.RegionDAOLocal;
import pe.gob.osce.sirenas.dao.convert.RegionConvert;
import pe.gob.osce.sirenas.model.AcreditacionDTO;
import pe.gob.osce.sirenas.model.RegionDTO;
import pe.gob.osce.sirenas.model.jpa.Region;
import pe.gob.osce.sirenas.util.ConstanteEJB;
import pe.gob.osce.sirenas.util.ModeloException;



@Stateless
public class RegionDAOImpl implements RegionDAOLocal {

	@PersistenceContext(unitName="pe.gob.osce.sirenas.be.pu" )
	private	EntityManager	em;
	ResourceBundle propSirenas = ResourceBundle.getBundle(ConstanteEJB.PROPERTIES);

	@Override 
	public List<RegionDTO>  ListarRegion() throws Exception {

		List<RegionDTO> listRegionDTO=null;
		List<Region> listaRegionJPA=null;
		try{
			StringBuilder  	jpql= new StringBuilder();
			jpql.append("SELECT rg FROM Region rg");
		
			Query	query= em.createQuery( jpql.toString() );
			
			List<Region>	listaObjeto= query.getResultList();
			if(!listaObjeto.isEmpty()) {
				listaRegionJPA=new ArrayList<Region>();
				for (Region obj: listaObjeto) {
					listaRegionJPA.add(obj);
				}
				listRegionDTO=RegionConvert.createListDTO(listaRegionJPA);
			}			
			
		} catch (NoResultException nre) {
			listRegionDTO=null;
			 throw new ModeloException(propSirenas.getString(ConstanteEJB.EXC_MODELO_NO_DATA));
		}catch (Exception e) {
			throw new ModeloException(propSirenas.getString(ConstanteEJB.EXC_MODELO_GENERAL_DB), e);
		}
		return listRegionDTO;
	}
	
}
